﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
using System.Text.RegularExpressions;
namespace Web
{
    public partial class ChartScoreAnswer : System.Web.UI.Page
    {
        protected int examroomid = 0, studentid = 0;
        protected static ExamPaperInfo ExamPaper_Info = new ExamPaperInfo();
        protected bool isquery = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "StatistScore"))
            {
                if (!UserValidate.CheckQuery())
                {
                    Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                    Response.Redirect("login.aspx");
                    Response.End();
                }
                else
                {
                    isquery = true;
                }
            }
            if (isquery)
            {
                studentid = int.Parse(HttpContext.Current.Session["QueryStudentId"] as string);
            }
            else if (!PageValidate.QueryStringIsEmpty("studentid"))
            {
                try
                {
                    studentid = int.Parse(PageValidate.QueryString("studentid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!PageValidate.QueryStringIsEmpty("examroomid"))
            {
                try
                {
                    examroomid = int.Parse(PageValidate.QueryString("examroomid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!IsPostBack)
            {
                if (examroomid > 0 && studentid > 0)
                {
                    ExamRoomInfo examroomInfo = ExamRoom.GetSingleInfo(examroomid);
                    if (examroomInfo != null)
                    {
                        ExamPaper_Info = BAL.ExamPaper.GetT1(examroomInfo.ExamPaperId);
                        LoadQuestion(examroomInfo.ExamPaperId);
                    }
                }
            }
        }

        private void LoadQuestion(int exampaperid)
        {
            string[] hanzi = new string[] { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十" };
            IList<PaperQuestionInfo> infos = new List<PaperQuestionInfo>();
            infos = Question.GetList(exampaperid);
            if (ExamPaper_Info.TestPaperPolicyMainId == null)
            {
                ExamPaper_Info.TestPaperPolicyMainId = 0;
            }
            IList<TestPaperPolicySubInfo> tpinfo = TestPaperPolicySub.GetList((int)ExamPaper_Info.TestPaperPolicyMainId);
            StringBuilder sb = new StringBuilder();
            int i = 0;
            int a = 1;
            foreach (TestPaperPolicySubInfo info in tpinfo)
            {
                XmlQuestion xq = XmlQuestionHelper.GetXmlQuestionInfo(info.Content);
                sb.Append("<h3>");
                sb.Append(hanzi[i]);
                sb.Append("、");
                sb.Append(QuesType.GetQuesTypeInfoById(int.Parse(xq.Type)).Name);
                sb.Append("共 ");
                sb.Append(xq.AllCount.ToString());
                sb.Append(" 小题 ");
                if (xq.SingleScore != null && xq.SingleScore.Count > 0)
                {
                    sb.Append("每小题 ");
                    sb.Append(string.Join(",", ArrayHelper.ConvertToStringArray(GetArray<decimal>.Get(xq.SingleScore))));
                    sb.Append(" 分");
                }
                sb.Append("总分为：");
                sb.Append(xq.AllScore.ToString());
                sb.Append("分");
                sb.Append("</h3>");
                IList<PaperQuestionInfo> tInfos = infos.Where(t => t.SubPolicyId == info.Id).ToList();
                foreach (PaperQuestionInfo qinfo in tInfos)
                {
                    sb.Append("<div class='block'>");
                    sb.Append("<span class='num'>" + a.ToString() + "、</span>");
                    sb.Append("<div class='content'>");
                    qinfo.Title = ClearHtml(qinfo.Title);
                    if (xq.SingleScore.Count > 1)
                    {
                        qinfo.Title = WiterScore(qinfo.Title, qinfo.Score);
                    }
                    sb.Append(qinfo.Title);
                    sb.Append("</div>");
                    sb.Append("<div class='answer'><span class='num'>参考答案：</span>");
                    sb.Append("<div class='content'>");
                    sb.Append(qinfo.Answer);
                    sb.Append("</div></div>");
                    sb.Append("<div class='isclear'></div></div>");
                    a++;
                }
                i++;
            }
            TempList.InnerHtml = sb.ToString();
        }

        private string WiterScore(string InputStr, decimal Score)
        {
            string t = InputStr;
            string[] strArray = new Regex("<.[^>]*?>(.*?)</.*?>", RegexOptions.IgnoreCase).Split(t);
            foreach (string item in strArray)
            {
                string temp = CodingHtml(item);
                temp = Regex.Replace(temp, "{{.*?}}", "");
                if (temp.Trim().Length > 0)
                {
                    if (!temp.Trim().StartsWith("{{"))
                    {
                        if (t.IndexOf(temp.Trim()) > -1)
                        {
                            t = t.Replace(temp, "(" + Score.ToString() + ")" + temp);
                            return t;
                        }
                    }
                }
            }
            return t;
        }

        /// <summary>
        /// 编码Html
        /// </summary>
        /// <param name="InputStr">输入字符串</param>
        /// <returns></returns>
        private string CodingHtml(string InputStr)
        {
            if (InputStr.Length > 0)
            {
                string t = InputStr;
                t = t.Replace("&nbsp;", "{{空格}}");
                t = Regex.Replace(t, "<(.*?)>", "{{($1)}}");
                return t;
            }
            else
            {
                return InputStr;
            }
        }

        /// <summary>
        /// 解码Html
        /// </summary>
        /// <param name="InputStr"></param>
        /// <returns></returns>
        private string DecodingHtml(string InputStr)
        {
            if (InputStr.Length > 0)
            {
                string t = InputStr;
                t = t.Replace("{{空格}}", "&nbsp;");
                t = Regex.Replace(t, "{{(.*?)}}", "<$1>");
                return t;
            }
            else
            {
                return InputStr;
            }
        }

        private string ClearHtml(string InputStr)
        {
            string Temp = Module_String.clearhtml(InputStr, "span");
            Temp = Temp.Replace("<p>&nbsp;</p>", "");
            Temp = Module_String.DelStr("style=\"", "\"", Temp);
            Temp = Module_String.DelStr("class=\"", "\"", Temp);
            return Temp;
        }
    }
}
